package com.cloudlinkscm.base.authcenter.assembler;

import com.cloudlinkscm.base.authcenter.constant.AuthCenterService;
import com.cloudlinkscm.base.authcenter.constant.ResourceTypeEnum;
import com.cloudlinkscm.base.authcenter.model.TenantResource;
import com.cloudlinkscm.base.authcenter.model.dto.PermissionTenantResourceCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.SubTenantResourceCreateDto;
import com.cloudlinkscm.base.authcenter.model.dto.TenantResourceCreateDto;
import com.cloudlinkscm.loms.framework.core.pojo.SingleAssembler;
import org.springframework.stereotype.Component;

/**
 * @author : tac
 * @date : 31/07/2017
 */

@Component
public class TenantResourceCreateDto2TenantResourceAssembler implements SingleAssembler<TenantResourceCreateDto, TenantResource> {
    @Override
    public TenantResource assemble(TenantResourceCreateDto from) {
        TenantResource to = new TenantResource();
        to.setResourceName(from.getResourceName());
        to.setResourceEnName(from.getResourceEnName());
        if(from instanceof SubTenantResourceCreateDto){
            to.setPid(((SubTenantResourceCreateDto) from).getPid());

            if(from instanceof PermissionTenantResourceCreateDto){
                to.setResourceKey(((PermissionTenantResourceCreateDto) from).getResourceKey());
            }
        }else{
            to.setPid(AuthCenterService.ROOT_RESOURCE_ID);
        }

        return to;
    }

    public TenantResource assemble(TenantResourceCreateDto from, ResourceTypeEnum type) {
        TenantResource to = assemble(from);
        to.setResourceType(type.code);
        return to;
    }
}
